package com.xqls.util;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.tools.Scope;
import org.apache.velocity.tools.ToolContext;
import org.apache.velocity.tools.ToolManager;
import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.view.ViewToolContext;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.StringWriter;
import java.util.Map;

/**
 * Created by Skeyedu on 2016/11/17.
 */
public class VelocityUtil {
    private static VelocityConfigurer velocityConfigurer;

    private  static ToolManager toolManagerLocal=null;




    public VelocityConfigurer getVelocityConfigurer() {
        return velocityConfigurer;
    }

    public void setVelocityConfigurer(VelocityConfigurer velocityConfigurer) {
        VelocityUtil.velocityConfigurer = velocityConfigurer;
    }

    public static VelocityEngine getConfigurer() {
        return velocityConfigurer.getVelocityEngine();
    }



    public static String getHtmlByMap(String vmPath, Map<String, Object> map,HttpServletRequest request, HttpServletResponse response, ServletContext application) {
        ViewToolContext context = new ViewToolContext(velocityConfigurer.getVelocityEngine(),request, response,
                application);
        ToolManager toolManager=getToolManager(request);
        if (toolManager.getToolboxFactory().hasTools(Scope.APPLICATION)) {
            context.addToolbox(toolManager.getToolboxFactory().createToolbox(
                    Scope.APPLICATION));
        }
        if (toolManager.getToolboxFactory().hasTools(Scope.SESSION)) {
            context.addToolbox(toolManager.getToolboxFactory().createToolbox(
                    Scope.SESSION));
        }
        if (toolManager.getToolboxFactory().hasTools(Scope.REQUEST)) {
            context.addToolbox(toolManager.getToolboxFactory().createToolbox(
                    Scope.REQUEST));
        }
        for(Map.Entry<String,Object> entry:map.entrySet()){
            context.put(entry.getKey(),entry.getValue());
        }
        StringWriter sw = new StringWriter();
        VelocityEngine engine = velocityConfigurer.getVelocityEngine();
        engine.mergeTemplate(vmPath, "UTF-8", context, sw);
        return sw.toString();
    }


    private  static  ToolManager getToolManager(HttpServletRequest request){
        if(toolManagerLocal==null){
            toolManagerLocal = new ToolManager();

            toolManagerLocal.configure(request.getSession().getServletContext().getRealPath("/")+"WEB-INF/conf/pro/toolbox.xml");
        }


        return toolManagerLocal;
    }



}
